首页
首页

Centos 安装 多规则替换过滤nginx模块nginx_substitutions_filter

-简介:ngx_http_substitutions_filter_module 是nginx的一个第三方模块,此模块作用是替换过滤响应主体,nginx也有一个类似这样的原生模块,但有一个缺点,就是只能使用一条规则,而nginx_substitutions_filter则不限规则数量。

-安装方法

1、下载nginx源码

1
2
cd /tmp/
wget http://nginx.org/download/nginx-1.8.1.tar.gz

2、下载ngx+http_substitutions_filter_module

1
2
cd /usr/local/
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module

3、查看现有nginx已安装的模块

1
niginx -V

以下为执行结果

1
2
3
4
5
6
[root@www ~]# nginx -V
nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/data/nginx --user=nobody --group=nobody --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi

4、解压nginx压缩包重新编译

1
2
3
4
5
6
cd /tmp/
tar -zxf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/data/nginx --user=nobody --group=nobody --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --add-module=/usr/local/ngx_http_substitutions_filter_module
make
make install

5、检查是否安装成功

1
niginx -V
1
2
3
4
5
6
[root@www ~]# nginx -V
nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/data/nginx --user=nobody --group=nobody --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --add-module=/usr/local/ngx_http_substitutions_filter_module

发现已经有ngx_http_substitutions_filter_module表示安装完成。

6、使用方法:

有两条指令:subs_filter_types,subs_filter

-subs_filter_types

语法: subs_filter_types mime-type [mime-types]

默认:subs_filter_types text/html

适用区域:http, server, location

subs_filter_types是用来指令需要替换的文件类型,默认是text/html类型。此模块无法处理经过压缩的内容,虽然能与gzip filter模块兼容,但无法处理反向代理返回的内容。当需要处理反向代理的内容时,可以使用如下语句禁用压缩:

1
proxy_set_header Accept-Encoding "";

-subs_filter

语法;subs_filter 源字段串 目标字段串 [gior]

默认:无

适用区域:http, server, location

subs_filter指令允许在nginx响应输出内容时替换源字段串(正则或固定)为目标字符串。
第三个标志含意如下:

g(默认): 替换所有匹配的字段串。
i: 执行区分大小写的匹配。
o: 仅替换首个匹配字符串。
r: 使用正则替换模式,默认是固定模式。

更详细的使用方法请访问:https://github.com/yaoweibin/ngx_http_substitutions_filter_module

支持一下
扫一扫,我会更有动力更新
  • 微信扫一扫
  • 支付宝扫一扫